﻿
//***********************************************************************
// * Module:   TbBstNoticeHandler
// * 作者:  曹荣芝
// * Modified: 2013/5/6
// * Purpose: Definition of the Class TbBstNoticeHandler 业务逻辑处理Handler类
// ***********************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using XWG.Framework.MSException;
using XWG.Framework.AuthUtility;
using XWG.Framework.AuthEntity;
using XWG.Framework.AuthSPConst;
using XWG.Framework.AuthBLL;
using XWG.JWBST.JWBSTEntity;
using XWG.JWBST.JWBSTSPConst;
using System.Web.Script.Serialization;
using System.Data;
using XWG.Framework.Query;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using XWG.JWBST.JWBSTBLL;
using XWG.Framework;
using System.Collections;
using JWBSTAPP.CommonClass;
namespace JWBSTAPP.AjaxServer
{
    /// <summary>
    /// TbBstNoticeHandler 的摘要说明
    /// </summary>
    public class TbBstNoticeHandler : JWBSTAPP.CommonClass.HttpHandlerBase, System.Web.SessionState.IRequiresSessionState
    {

        #region 添加记录
        /// <summary>
        /// 添加记录
        /// </summary>
        protected void AddRecord()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            try
            {
                var mUser = AppOrganizatiOnUserBLL.Load(context.Session["UserID"].ToString());
                TbBstNotice mTbBstNotice = new TbBstNotice();
                mTbBstNotice.Id = Guid.NewGuid().ToString(); //主键
                mTbBstNotice.Title = context.Request.Form["texTitle"]; //标题
                mTbBstNotice.Content = context.Request.Form["texContent"]; //内容
                mTbBstNotice.Createuser = mUser.UserName; //添加人
                mTbBstNotice.Createdate = DateTime.Now; //添加时间
                mTbBstNotice.Deltag = 0; //删除标志
                mTbBstNotice.Auditsign = 0; //审核状态
                mTbBstNotice.Istop = context.Request.Form["selIstop"];//是否置顶
                //mTbBstNotice.Deltime = DateTime.Parse(context.Request.Form["texDeltime"]); //删除时间
                //  mTbBstNotice.Modifydate = DateTime.Parse(context.Request.Form["texModifydate"]); //修改时间

                mResultModel = TbBstNoticeBLL.Insert(mTbBstNotice, context.Session["UserID"], true); //新建操作
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, "");
                mResultModel.ErrorCode = -9999;
                mResultModel.ISSucceed = false;
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
        }
        #endregion

        #region 删除记录
        /// <summary>
        /// 删除记录
        /// </summary>
        protected void DeleteRecord()
        {

            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //声明返回类型
            try
            {
                string[] arryParamKey = context.Request.Params["id"].ToString().Split(',');
                if (arryParamKey.Length == 0)
                {
                    mResultModel.ErrorCode = -9991;
                    mResultModel.ISSucceed = false;
                    context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回    
                    return;
                }
                TbBstNotice mTbBstNotice = new TbBstNotice();
                foreach (var paramKey in arryParamKey)
                {
                    mResultModel = TbBstNoticeBLL.Delete(paramKey, context.Session["UserID"], true); //删除对应的信息
                }
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); ; //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, ""); //写入异常日志
                mResultModel.ErrorCode = -9999; //-9999代码是系统异常通用代码
                mResultModel.ISSucceed = false; //行为执行失败
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }

        }
        #endregion
        #region 修改记录
        /// <summary>
        /// 修改记录
        /// </summary>
        protected void EditRecord()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            TbBstNotice mTbBstNotice = new TbBstNotice();

            string paramKey = context.Request.Params["id"];
            try
            {

                if (string.IsNullOrEmpty(paramKey))
                {
                    mResultModel.ErrorCode = -9991;
                    mResultModel.ISSucceed = false;
                    context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回    
                    return;
                }
                mTbBstNotice = TbBstNoticeBLL.Load(paramKey); //根据键值获取实体信息
                if (mTbBstNotice != null)
                {
                    var mUser = AppOrganizatiOnUserBLL.Load(context.Session["UserID"].ToString());
                    mTbBstNotice.Title = context.Request.Form["texTitle"]; //标题
                    mTbBstNotice.Content = context.Request.Form["texContent"]; //内容
                    mTbBstNotice.Createuser = mUser.UserName; //添加人
                    mTbBstNotice.Istop = context.Request.Form["selIstop"];//是否置顶
                    //mTbBstNotice.Createdate = DateTime.Parse(context.Request.Form["texCreatedate"]); //添加时间
                    mTbBstNotice.Deltag = 0; //删除标志
                    //mTbBstNotice.Deltime = DateTime.Parse(context.Request.Form["texDeltime"]); //删除时间
                    mTbBstNotice.Modifydate = DateTime.Now; //修改时间
                    mTbBstNotice.Auditsign = 0; //审核状态

                    mResultModel = TbBstNoticeBLL.Update(mTbBstNotice, context.Session["UserID"], true); //新建操作
                }
                else
                {
                    mResultModel.ErrorCode = -9990; //没有获取所需的信息，信息可能已被删除
                    mResultModel.ISSucceed = false;

                }
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, "");
                mResultModel.ErrorCode = -9999;
                mResultModel.ISSucceed = false;
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
        }
        #endregion

        #region 获取实体记录
        /// <summary>
        /// 获取实体记录
        /// </summary>
        protected void GetModel()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            string paramKey = context.Request.Params["id"];
            string json = "";
            try
            {
                TbBstDic mBaseDic = new TbBstDic();
                TbBstNotice mTbBstNotice = new TbBstNotice(); //声明实体
                if (!string.IsNullOrEmpty(paramKey))
                {
                    mTbBstNotice = TbBstNoticeBLL.Load(paramKey); //根据键值获取管理员
                    if (mTbBstNotice != null)
                    {
                        mTbBstNotice.AuditsignStr = mTbBstNotice.Auditsign.ToString() == "0" ? "未审核" : "已审核";
                        if (mTbBstNotice.Modifydate.ToString("yyyy") == "0001")
                        {
                            mTbBstNotice.Modifydatestr = "暂无修改";
                        }
                        else
                        {
                            mTbBstNotice.Modifydatestr = mTbBstNotice.Modifydate.ToString("yyyy-MM-dd HH:mm:ss");
                        }
                        IsoDateTimeConverter dtConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };
                        json = JsonConvert.SerializeObject(mTbBstNotice, dtConverter);

                        //解析是否置顶
                        if (!String.IsNullOrEmpty(mTbBstNotice.Istop))
                        {
                            var list = TbBstBsznBLL.GetDicByCodeAndParentnode(mTbBstNotice.Istop, "Top");
                            mBaseDic = list.SingleOrDefault();
                            Hashtable hashtable = new Hashtable();
                            if (mBaseDic != null)
                            {
                                hashtable.Add("VIstop", mBaseDic.Description);
                                json = JsonString.AddAttribute(json, hashtable);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, "");
                mResultModel.ErrorCode = -9990;
                mResultModel.ISSucceed = false;
                json = JsonConvert.SerializeObject(mResultModel);
            }
            context.Response.Write(json);
        }
        #endregion

        #region 获取数据
        /// <summary>
        /// 获取数据记录
        /// </summary>
        protected void QueryRecord()
        {
            TableMessage tm = new TableMessage();
            HearderMessage hm = new HearderMessage();
            List<HearderMessage> list = new List<HearderMessage>();
            List<TbBstNotice> nlist = new List<TbBstNotice>();
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            string pageSize = context.Request.Form["pageSize"];
            string pageIndex = context.Request.Form["pageIndex"];
            string title = context.Request.Form["texTitle"];////标题
            string name = context.Request.Form["texCreateUser"];////发布人
            string auditsign = context.Request.Form["selAuditsign"]; //审核状态
            string timeStart = context.Request.Form["texTimeStart"]; //开始时间
            string timeEnd = context.Request.Form["texTimeEnd"]; //结束时间
            string top = context.Request.Form["Seltop"];//置顶状态
            if (String.IsNullOrEmpty(pageSize) || String.IsNullOrEmpty(pageIndex) || !AnyString.IsNumeric(pageSize) || !AnyString.IsNumeric(pageIndex))
                return;
            DataTable dtTbBstNotice = new DataTable();
            int intPageSize = Convert.ToInt32(pageSize);
            int intPageIndex = Convert.ToInt32(pageIndex);
            QueryByPage Q = new QueryByPage();
            QueryInfo qInfo = new QueryInfo();

            SqlConditionList sqllist = new SqlConditionList();
            string sqlWhere = "deltag=0";

            if (!string.IsNullOrEmpty(title))////标题
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.LIKE, TbBstNoticeConst.Columns.Title, '%' + title + '%'), SqlLogicOper.AND_LOGIC);
            }

            if (!string.IsNullOrEmpty(top))
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.EQUAL, TbBstNoticeConst.Columns.Istop, top), SqlLogicOper.AND_LOGIC);
            }
            if (!string.IsNullOrEmpty(name))////发布人
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.LIKE, TbBstNoticeConst.Columns.Createuser, '%' + name + '%'), SqlLogicOper.AND_LOGIC);
            }
            if (!string.IsNullOrEmpty(auditsign))////审核状态
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.EQUAL, TbBstNoticeConst.Columns.Auditsign, auditsign), SqlLogicOper.AND_LOGIC);
            }

            if (!string.IsNullOrEmpty(timeStart)) ////开始时间
            {
                DateTime dtstart = new DateTime();
                if (DateTime.TryParse(timeStart, out dtstart))
                {
                    sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.NOT_LESS_THAN, TbBstNoticeConst.Columns.Createdate, dtstart), SqlLogicOper.AND_LOGIC);
                }
            }
            if (!string.IsNullOrEmpty(timeEnd))
            {
                DateTime dtend = new DateTime();
                if (DateTime.TryParse(timeEnd, out dtend))
                {
                    sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.NOT_MORE_THAN, TbBstNoticeConst.Columns.Createdate, dtend), SqlLogicOper.AND_LOGIC);
                }
            }
            if (string.IsNullOrEmpty(sqllist.ToSql()))
            {
                sqlWhere = "where " + sqlWhere;
            }
            else
            {
                sqlWhere = sqllist.ToSql() + " and " + sqlWhere;
            }

            dtTbBstNotice = Q.GetRecordByPage("ID,TITLE,CREATEUSER,AUDITSIGN,CREATEDATE", sqlWhere, "ISTOP,CREATEDATE", intPageSize, intPageIndex, TbBstNoticeConst.TableName, TbBstNoticeConst.FieldKey, "desc").Tables[0];

            //dtTbBstNotice = Q.GetRecordByPage("*", "", "", intPageSize, intPageIndex, TbBstNoticeConst.TableName, TbBstNoticeConst.FieldKey, "").Tables[0];
            if (dtTbBstNotice != null && dtTbBstNotice.Rows.Count > 0)
            {
                for (int i = 0; i < dtTbBstNotice.Columns.Count; i++)
                {
                    hm = new HearderMessage();
                    hm.name = dtTbBstNotice.Columns[i].Caption;
                    list.Add(hm);
                }
                for (int i = 0; i < dtTbBstNotice.Rows.Count; i++)
                {
                    var model = new TbBstNotice();
                    model.Id = dtTbBstNotice.Rows[i][TbBstNoticeConst.Columns.Id].ToString();
                    model.Createdate = DateTime.Parse(dtTbBstNotice.Rows[i][TbBstNoticeConst.Columns.Createdate].ToString());
                    model.Title = dtTbBstNotice.Rows[i][TbBstNoticeConst.Columns.Title].ToString();
                    model.Createuser = dtTbBstNotice.Rows[i][TbBstNoticeConst.Columns.Createuser].ToString();
                    //  model.Content = dtTbBstNotice.Rows[i][TbBstNoticeConst.Columns.Content].ToString();
                    model.AuditsignStr = dtTbBstNotice.Rows[i][TbBstNoticeConst.Columns.Auditsign].ToString() == "0" ? "未审核" : "已审核";
                    nlist.Add(model);
                }
                tm.Header = list;
                tm.data = nlist;

                tm.totalCount = Convert.ToString((int)Q.RowCount);
            }
            IsoDateTimeConverter dtConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };
            string json = JsonConvert.SerializeObject(tm, dtConverter);
            context.Response.Write(json);
        }
        #endregion

        #region 审核记录
        /// <summary>
        /// 审核记录
        /// </summary>
        protected void AuditRecord()
        {

            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //声明返回类型
            try
            {
                string[] arryParamKey = context.Request.Params["id"].ToString().Split(',');
                if (arryParamKey.Length == 0)
                {
                    mResultModel.ErrorCode = -9991;
                    mResultModel.ISSucceed = false;
                    context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回    
                    return;
                }
                foreach (var paramKey in arryParamKey)
                {
                    var mTbBstNotice = TbBstNoticeBLL.Load(paramKey); //根据键值获取管理员
                    mTbBstNotice.Auditsign = 1;
                    mResultModel = TbBstNoticeBLL.Update(mTbBstNotice, context.Session["UserID"], true); //新建操作
                }
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); ; //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, ""); //写入异常日志
                mResultModel.ErrorCode = -9999; //-9999代码是系统异常通用代码
                mResultModel.ISSucceed = false; //行为执行失败
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }

        }
        #endregion

    }
}


